package com.example.courselab

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.courselab.ui.theme.CourseLabTheme
import com.example.courselab.ui.theme.GreenJC
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class SplashActivity : ComponentActivity() {
  @SuppressLint("CoroutineCreationDuringComposition")
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // 检查是否是首次启动
    val isFirstStart =
      getSharedPreferences("AppPrefs", MODE_PRIVATE).getBoolean("isFirstStart", true)

    setContent {
      CourseLabTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
          if (isFirstStart) {
            SplashScreen(modifier = Modifier.padding(innerPadding))
          } else {
            startActivity(Intent(this@SplashActivity, MainActivity::class.java))
            finish()
          }
        }
      }
    }
  }
  @Composable
  fun SplashScreen(modifier: Modifier = Modifier) {
    var time by remember { mutableIntStateOf(5) }
    val transition = rememberInfiniteTransition()
    val coroutineScope = rememberCoroutineScope()
    val progress by transition.animateFloat(
      initialValue = 1f,
      targetValue = 0f,
      animationSpec = infiniteRepeatable(
        tween(durationMillis = 5000, easing = LinearEasing),
        repeatMode = RepeatMode.Restart
      )
    )
    Surface(color = MaterialTheme.colorScheme.background) {
      Box(
        contentAlignment = Alignment.Center,
        modifier = modifier.fillMaxSize()
      ) {
        Image(
          painter = painterResource(id = R.drawable.splash),
          contentDescription = "Splash Image",
          modifier = Modifier.fillMaxSize(),
          contentScale = ContentScale.Crop
        )
        LaunchedEffect(key1 = Unit) {
          coroutineScope.launch {
            while (time > 0) {
              delay(1000)
              when (time) {
                2 -> {
//              getSharedPreferences("AppPrefs", MODE_PRIVATE).edit {
//                putBoolean("isFirstStart", false)
//              }
                }
                1 -> {
                  startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                  finish()
                }
              }

              time--
            }
          }
        }
        Box(
          contentAlignment = Alignment.Center,
          modifier = Modifier
            .size(50.dp)
            .align(Alignment.TopEnd)
            .padding(end = 16.dp, top = 16.dp)
        ) {
          CircularProgressIndicator(
            progress = progress,
            color = GreenJC,
            strokeWidth = 4.dp
          )
          Text(
            text = time.toString(),
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold,
            color = Color.White
          )
        }
      }
    }
  }
}
